<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css" />
</head>
<body>
<body class="pear-container">
    <div class="mainBox">
        <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" action="">
                <div class="layui-form-item">
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label">账号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="username" placeholder="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                    </div>
                </div>
            </form>
            <table id="user-table" lay-filter="user-table"></table>
        </div>
    </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" id="dept-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
        </div>
    </div>
</body>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
layui.use(['form','jquery','select','table'],function(){
    let form = layui.form;
    let $ = layui.jquery;
    let select = layui.select;
    let table = layui.table;
    let deptId;

    let cols = [
        [
            {
                type: 'checkbox'
            },
            {
                title: '账号',
                field: 'username',
                align: 'center',
                width: 100
            },
            {
                title: '姓名',
                field: 'realName',
                align: 'center'
            },
            {
                title: '电话',
                field: 'phone',
                align: 'center'
            }
        ]
    ]

    table.render({
        elem: '#user-table',
        url: '/user/list?hasDept=0',
        page: true,
        cols: cols,
        skin: 'line',
        id: 'userId',
        defaultToolbar: []
    });

    form.on('submit(user-query)', function(data) {
        table.reload('user-table', {
            where: data.field
        })
        return false;
    });

    $("#dept-save").click(function(){
        let checkStatus = table.checkStatus('userId'); //idTest 即为基础参数 id 对应的值
        let dataArr = checkStatus.data;
        if (dataArr.length == 0) {
            layer.msg("未选中数据", {
                icon: 3,
                time: 1000
            });
            return;
        }
        let idArr = dataArr.map(item => item.userId)
        let formData = {
            'deptId':deptId,
            'userIds':idArr
        }
        $.ajax({
            url: "/batch-create/user/dept",
            dataType: 'json',
            data:JSON.stringify(formData),
            type: 'post',
            contentType:'application/json',
            success: function(result) {
                layer.msg('用户分配部门成功',{icon:1,time:1000},function(){
                    parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                    parent.window.refresh();
                });
            },
            error: function (data) {
                layer.close(loading);
                layer.msg(data.responseJSON.detail,{icon:2,time:1000});
            }
        })
    });

    window.init = function (obj) {
        deptId = obj
    }

})
</script>
</body>
</html>
